package com.asset.salesforce.validator;

import java.util.List;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.springframework.beans.factory.annotation.Autowired;

import com.asset.salesforce.domain.StockSubCategory;
import com.asset.salesforce.form.StockSubCategoryForm;
import com.asset.salesforce.service.InventoryService;

public class SubCategoryNameNotExistConstraintValidator implements ConstraintValidator<SubCategoryNameNotExist, Object>{
	@Autowired
	private InventoryService inventoryService;
	
	@Override
	public void initialize(SubCategoryNameNotExist arg0) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public boolean isValid(Object arg0, ConstraintValidatorContext arg1) {
		StockSubCategoryForm subCategoryForm = (StockSubCategoryForm)arg0;
		boolean success = false; 
		List<StockSubCategory> subCategories = inventoryService.getAllSubCategoriesByCategory(subCategoryForm.getParentId(),subCategoryForm.getEmployeeId());
		if(subCategories.size()==0){
			success=true;
		}
		else{
		for (StockSubCategory stockSubCategory : subCategories) {
			if(!stockSubCategory.getName().equals(subCategoryForm.getName())){
				success=true;
			}else{
				success=false;
			}
		}
		}
		return success;
	   }
	
}
